<template>
    <div class="shopcar-container">
        <!-- 商品列表区域 -->
        <div class="shopcar-list">
            <div class="mui-card" v-for="(item,i) in shopCarList" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch 
                        v-model="$store.getters.getGoodsSelected[item.id]"
                        @change="selectedChanged(item.id)">
                        </mt-switch>
                        <img src="/src/images/goodslist/goods1.jpg" alt="">
                        <div class="info">
                            <h3>{{ item.title }}</h3>
                            <div class="info-content">
                                <span>${{ item.price }}</span>
                                <numbox 
                                :initCount="item.count" :goodsid="item.id"></numbox>
                                <a href="" @click.prevent="remove(item.id,i)">删除</a>
                            </div>    
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 结算区域 -->
        <div class="shopcar-pay">
            <div class="mui-card">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <div class="left">
                            <p>总计(不含运费)</p>
                            <p>已勾选商品：<span>{{ $store.getters.getGoodsCountAndAmount.count }}</span>件,
                            总计：<span>${{ $store.getters.getGoodsCountAndAmount.amount }}</span>
                            </p>
                        </div>
                        <mt-button type="danger">去结算</mt-button>
                    </div>
                </div>
            </div>
        </div>
        <p>{{ $store.getters.getGoodsSelected}} </p>
    </div>
</template>

<script>
    import numbox from '../subcomponents/shopCar_numberbox.vue'
    var shopCarList=[];
    
    export default{
        data(){
            return{
                shopCarList:shopCarList
            }
        },
        beforeCreate(){
            //数据观测之前，先把本地存储的car取出来 赋值给shopCarList
            //本步骤执行完毕后 data中的数据才开始观测
            shopCarList=JSON.parse(localStorage.getItem('car'));
        },
        methods:{
            remove(id,index){
                this.shopCarList.splice(index,1);
                this.$store.commit("removeFormCar",id)
            },
            selectedChanged(id){
                this.$store.commit("updateGoodsSelected",id)
            }
        },
        components:{
            numbox
        }
    }
</script>

<style lang="less" scoped>
    .shopcar-container{
        background-color: #eee;
        overflow: hidden;
        .shopcar-list{
            img{
            width: 60px;
            height: 60px;
            }
            .mui-card-content-inner{
                display:  flex;
                justify-content: space-between;
                .info{
                    flex-grow: 1;
                    h3{
                    font-size: 14px;
                    }
                    .info-content{
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                        span{
                            color: red;
                            font-weight: bold;
                            vertical-align: bottom;
                        }
                    }
                }
            }
        }   
        .shopcar-pay{
            .mui-card-content-inner{
                display: flex;
                justify-content: space-between;
                align-items: center;
                span{
                    color:red;
                    font-weight: bold;
                    font-size: 16px;
                }
            }
        } 
    }
</style>